package Middlewares


import(
	"github.com/gin-gonic/gin"
	"gin/Common"
	"fmt"
)


var g_name = "zhanghao001"
var g_mima = "Qbydl_001"
var g_solt = "qsdsdfxisjdfosdfjsodf"

func Login(c *gin.Context) (string,string){
	var name = c.Query("name")
	var pwd  = c.Query("pwd")
	var login_cookie string
	fmt.Println("login, ", name, "|", pwd)
	fmt.Println("login, ", g_name, "|", g_mima)
	if ((name == g_name) && (pwd == g_mima)) {
		fmt.Println("login, ", name)
		login_cookie = Common.MD5Str(name + pwd + g_solt)
		c.SetCookie("login", login_cookie, 3600, "/", "admin.saybydata.com", false, true)
		c.SetCookie("name", name, 3600, "/", "admin.saybydata.com", false, true)
	}
	return login_cookie,name
}

func CheckLogin(c *gin.Context) bool {
	if (1 == Common.IsTest()) {
		return true
	}
	
	name,_ := c.Cookie("name")
	if (name != g_name) {
		return false
	}
	var login_cookie = Common.MD5Str(g_name + g_mima + g_solt)
	val, _ := c.Cookie("login")
	if (val == login_cookie) {
		return true
	}
	return false
}
